home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / ttylink.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  134 lines

  1. /* Internet TTY "link" (keyboard chat) server
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #include "mbuf.h"
  9. #include "socket.h"
  10. #include "telnet.h"
  11. #include "session.h"
  12. #include "proc.h"
  13. #include "tty.h"
  14. #include "mailbox.h"
  15. #include "commands.h"
  16.  
  17. #ifdef ALLSERV
  18. static int Sttylink = -1;    /* Protoype socket for service */
  19. extern int Attended;
  20. static char Tnbanner[] = "Welcome to TTY-Link at %s\n";
  21. extern char SysopBusy[];
  22. extern int Numrows,Numcols;
  23.  
  24. int
  25. ttylstart(argc,argv,p)
  26. int argc;
  27. char *argv[];
  28. void *p;
  29. {
  30.     struct sockaddr_in lsocket;
  31.     int s,type;
  32.  
  33.     if(Sttylink != -1){
  34.         return 0;
  35.     }
  36.     psignal(Curproc,0);    /* Don't keep the parser waiting */
  37.     chname(Curproc,"TTYlink listener");
  38.  
  39.     lsocket.sin_family = AF_INET;
  40.     lsocket.sin_addr.s_addr = INADDR_ANY;
  41.     if(argc < 2)
  42.         lsocket.sin_port = IPPORT_TTYLINK;
  43.     else
  44.         lsocket.sin_port = atoi(argv[1]);
  45.  
  46.     Sttylink = socket(AF_INET,SOCK_STREAM,0);
  47.     bind(Sttylink,(char *)&lsocket,sizeof(lsocket));
  48.     listen(Sttylink,1);
  49.     for(;;){
  50.         if((s = accept(Sttylink,NULLCHAR,(int *)NULL)) == -1)
  51.             break;    /* Service is shutting down */
  52.         
  53. #ifdef UNIX
  54.         if(!Attended){
  55. #else
  56.         /* Check for enough mem for new screen - WG7J */
  57.         if(availmem() < Memthresh+(2*Numrows*Numcols)){
  58.             usprintf(s,"System is overloaded; try again later\r\n");
  59.             close_s(s);
  60.         } else if(!Attended){
  61. #endif
  62.             usprintf(s,SysopBusy);
  63.             close_s(s);
  64.         } else {
  65.             type = TELNET;
  66.             if(newproc("chat",2048,ttylhandle,s,(void *)&type,NULL,0) == NULLPROC)
  67.                 close_s(s);
  68.         }
  69.     }
  70.     return 0;
  71. }
  72. /* This function handles all incoming "chat" sessions, be they TCP,
  73.  * NET/ROM or AX.25
  74.  */
  75. void
  76. ttylhandle(s,t,p)
  77. int s;
  78. void *t;
  79. void *p;
  80. {
  81.     int type,index;
  82.     struct session *sp;
  83.     char addr[MAXSOCKSIZE];
  84.     int len = MAXSOCKSIZE;
  85.     struct telnet tn;
  86.     extern char *Motd;
  87.     time_t nowtime;
  88.  
  89.     type = * (int *)t;
  90.     sockmode(s,SOCK_ASCII);
  91.     sockowner(s,Curproc);    /* We own it now */
  92.     log(s,"open %s",Sestypes[type]);
  93.  
  94.     /* Allocate a session descriptor */
  95.     if((sp = newsession(NULLCHAR,type,1)) == NULLSESSION){
  96.         tputs(TooManySessions);
  97.         close_s(s);
  98.         return;
  99.     }
  100.     index = sp - Sessions;
  101.  
  102.     /* Initialize a Telnet protocol descriptor */
  103.     memset((char *)&tn,0,sizeof(tn));
  104.     tn.session = sp;    /* Upward pointer */
  105.     sp->cb.telnet = &tn;    /* Downward pointer */
  106.     sp->s = s;
  107.     sp->proc = Curproc;
  108.  
  109.     time(&nowtime);        /* current time */
  110.  
  111.     getpeername(s,addr,&len);
  112.     tprintf("\007Incoming %s session %u from %s on %s",
  113.      Sestypes[type],index,psocket(addr),ctime(&nowtime));
  114.  
  115.     usprintf(s, Tnbanner, Hostname);
  116.     if(Motd != NULLCHAR)
  117.         usprintf(s, "%s", Motd);
  118.  
  119.     tnrecv(&tn);
  120. }
  121.  
  122. /* Shut down Ttylink server */
  123. int
  124. ttyl0(argc,argv,p)
  125. int argc;
  126. char *argv[];
  127. void *p;
  128. {
  129.     close_s(Sttylink);
  130.     Sttylink = -1;
  131.     return 0;
  132. }
  133. #endif /*ALLSERV*/
  134.